package com.infovane.action;

import java.util.List;

import com.infovane.base.BaseAction;
import com.infovane.model.PageModel;
import com.infovane.model.TArticle;
import com.infovane.model.TTopic;
import com.infovane.service.ArticleService;
import com.infovane.service.TopicService;
import com.infovane.utils.PagingModule;
import com.infovane.utils.SystemConstant;

public class IndexAction extends BaseAction{
	
	private ArticleService articleService;
	
	
	public ArticleService getArticleService() {
		return articleService;
	}
	
	private TopicService topicService;
	


	public TopicService getTopicService() {
		return topicService;
	}

	public void setTopicService(TopicService topicService) {
		this.topicService = topicService;
	}

	public void setArticleService(ArticleService articleService) {
		this.articleService = articleService;
	}

	/**
	 * 分页获取文章数据
	 * */
	public  void getArticleList(){
		String strCurrentPage=httpServletRequest.getParameter("currentPage");
		int currentPage;
		if(strCurrentPage==null||strCurrentPage.equals("")){
			currentPage=1;
		}else{
			try{
				currentPage=Integer.parseInt(strCurrentPage.intern());
			}catch(NumberFormatException e){
				currentPage=1;
			}
		}
		List<TArticle> list=this.articleService.getArticleList(currentPage,SystemConstant.PAGE_SIZE);
		int count=this.articleService.getCount();
		int maxPage=PagingModule.maxPageSize(count);
		PageModel<TArticle> pm=new PageModel<TArticle>(list,maxPage);
		httpServletRequest.setAttribute("pm", pm);
		
	}
	/**
	 * 获取设置了标题颜色的文章
	 * */
	public void getColorArticle(){
		TArticle article=new TArticle();
		List list=this.articleService.getColor(SystemConstant.ARTICLE_COLOR, SystemConstant.ARTICLE_CONSULT, SystemConstant.ARTICLE_AUTHORITY);
		if(list!=null&&list.size()>0){
			article=(TArticle) list.get(0);
			httpServletRequest.setAttribute("TCOLOR",article);
		}
		
	}
	
	/**
	 * 获取官方推荐文章
	 * */
	public void getAuthorityArticle(){
		List<TArticle> list=this.articleService.getGrade1();
		if(list!=null&&list.size()>0){
			httpServletRequest.setAttribute("TGRADE",list );
		}
	}
	
	/**
	 * 获取专题文章
	 * */
	public void getTopicList(){
		List<TTopic> list=this.topicService.getTopicList(0,SystemConstant.TOPIC_NUM);
		if(list!=null&&list.size()>0){
			httpServletRequest.setAttribute("TTOPIC", list);
		}
	}
	
	/**
	 * 获取热门资讯
	 * */
	public String getClickArticle(){
		getSumComment();
		List<TArticle> list=this.articleService.getClickArticle();
		if(list!=null&&list.size()>0){
			httpServletRequest.setAttribute("CTARTICLE", list);
			return SUCCESS;
		}
		return ERROR;
		
	}
	
	/**
	 * 获取评论最多的文章
	 * */
	public void getSumComment(){
		List<TArticle> list=this.articleService.getSumComment();
		if(list!=null&&list.size()>0){
			httpServletRequest.setAttribute("STARTICLE", list);
		}
	}
	
	/**
	 * 获取首页数据
	 * */
	@Override
	public String execute(){
		getTopicList();
		getArticleList();
		getColorArticle();
		getAuthorityArticle();
		return SUCCESS;
	} 

}
